package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.VehicleService;
import com.lsyw.intergrate.entity.Vehicle;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 车辆信息 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-29
 */
@Api(tags = "车辆信息")
@RestController
@RequestMapping("/vehicle")
public class VehicleController {


    @Autowired
    private VehicleService vehicleService;



    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody Vehicle vehicle) {
        vehicleService.save(vehicle);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            vehicleService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody Vehicle vehicle) {
        vehicleService.updateById(vehicle);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody VehicleReqVo vehicle) {
        final LambdaQueryWrapper<Vehicle> lambda = new QueryWrapper<Vehicle>().lambda();
        this.buildCondition(lambda, vehicle);
        return Result.successResult(vehicleService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<Vehicle> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<Vehicle> lambda = new QueryWrapper<Vehicle>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.vehicleService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Vehicle> lambda, VehicleReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(Vehicle::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getPlateNumber())) {
            lambda.eq(Vehicle::getPlateNumber, param.getPlateNumber());
        }
        if (!StringUtils.isEmpty(param.getXszUrlA())) {
            lambda.eq(Vehicle::getXszUrlA, param.getXszUrlA());
        }
        if (!StringUtils.isEmpty(param.getXszUrlB())) {
            lambda.eq(Vehicle::getXszUrlB, param.getXszUrlB());
        }
        if (!StringUtils.isEmpty(param.getDriverId())) {
            lambda.eq(Vehicle::getDriverId, param.getDriverId());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(Vehicle::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(Vehicle::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(Vehicle::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(Vehicle::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(Vehicle::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(Vehicle::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(Vehicle::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(Vehicle::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, Vehicle::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class VehicleReqVo extends Vehicle {
        private List<String> createdTimeList; // 创建时间起止
        private List<String> updatedTimeList; // 更新时间起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
